
/* ************************************************************************************* 
 * CREATED BY:          MYGENERATION
 * CREATED DATE:        1/16/2009
 * PROJECT:             MCWEBSITE
 * PURPOSE:             websiteResourceDAL class
 * MODIFIED BY:         None
 * MODIFIED DATE:       None
 * MODIFIED REASON:     None
 * NOTES:               None
 **************************************************************************************/
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Web.Configuration;
using MCWEBSITE.ENTITY;

namespace MCWEBSITE.DAL
{
    public class websiteResourceDAL
    {
        #region  Properties
		 
		public static string FIELD_RESOURCEID = "ResourceID";  
		public static string FIELD_LANGUAGECODE = "LanguageCode";  
		public static string FIELD_RESOURCECODE = "ResourceCode";  
		public static string FIELD_RESOURCETYPEID = "ResourceTypeID";  
		public static string FIELD_RESOURCENAME = "ResourceName";  
		public static string FIELD_DESCRIPTION = "Description";  
		public static string FIELD_CREATER = "Creater";  
		public static string FIELD_CREATEDATE = "CreateDate";  
		public static string FIELD_FILETYPE = "FileType";  
		public static string FIELD_FILEURL = "FileUrl";  
		public static string FIELD_DOWNLOADED = "Downloaded";  
		public static string FIELD_ISACTIVE = "IsActive";  
		public static string FIELD_ATRIBUTE1 = "Atribute1";  
		public static string FIELD_ATRIBUTE2 = "Atribute2";  
		public static string FIELD_ATRIBUTE3 = "Atribute3"; 
		public static string ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ContentConnectionString"].ToString();

        #endregion

        #region Protected methods

        /// <summary>
        /// Select entity of websiteResource
        /// </summary>
        /// <param name="ResourceID">Primary Keys</param>
        /// <returns>Collection of websiteResourceInfo</returns>
        public static websiteResourceInfo SelectOne(int ResourceID)
        {
            websiteResourceInfo _websiteResourceInfo = new websiteResourceInfo();
            SqlDataReader dr = null;
            try
            {
                dr = SqlHelper.ExecuteReader(ConnectionString, "sp_mc_websiteResourceSelectOne", ResourceID);
                if (dr.Read())
                    setwebsiteResourceInfo(dr, ref _websiteResourceInfo);
                return _websiteResourceInfo;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                dr.Close();
            }
        }

        /// <summary>
        /// Select all websiteResource
        /// </summary>
        /// <returns>Collection of websiteResourceInfo</returns>
        public static List<websiteResourceInfo> SelectAll()
        {
            List<websiteResourceInfo> lstwebsiteResource = new List<websiteResourceInfo>();
            SqlDataReader dr = null;
            try
            {
                dr = SqlHelper.ExecuteReader(ConnectionString, "sp_mc_websiteResourceSelectAll");
                setListwebsiteResourceInfo(ref dr, ref lstwebsiteResource);
                return lstwebsiteResource;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
            }
        }

        public static List<websiteResourceInfo> SelectByLanguage(string status, string LanguageCode)
        {
            List<websiteResourceInfo> lstwebsiteResource = new List<websiteResourceInfo>();
            SqlDataReader dr = null;
            try
            {
                dr = SqlHelper.ExecuteReader(ConnectionString, "sp_mc_websiteResourceSelectByLanguage", status, LanguageCode);
                setListwebsiteResourceInfo(ref dr, ref lstwebsiteResource);
                return lstwebsiteResource;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
            }
        }

        public static List<websiteResourceInfo> GetByStatus(string status)
        {
            List<websiteResourceInfo> lstwebsiteResource = new List<websiteResourceInfo>();
            SqlDataReader dr = null;
            try
            {
                dr = SqlHelper.ExecuteReader(ConnectionString, "sp_mc_websiteResourceSelectByStatus", status);
                setListwebsiteResourceInfo(ref dr, ref lstwebsiteResource);
                return lstwebsiteResource;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
            }
        }

        /// <summary>
        /// Select
        /// </summary>
        /// <param name="websiteResourcenfo">Modify this method adapting your need</param>
        /// <returns>Collection of websiteResourceInfo</returns>
        public static List<websiteResourceInfo> SelectFilters()
        {
            string criterias = string.Empty;
            List<websiteResourceInfo> lstwebsiteResource = new List<websiteResourceInfo>();
            SqlDataReader dr = null;
            try 
            {
                dr = SqlHelper.ExecuteReader(ConnectionString, "sp_mc_websiteResourceSelectByFilters");
                setListwebsiteResourceInfo(ref dr, ref lstwebsiteResource);
                return lstwebsiteResource;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
            }
        }

        /// <summary>
        /// Insert websiteResource
        /// </summary>
        /// <param name="websiteResourceInfo">Entity of websiteResource</param>
        /// <returns>error code</returns>
        public static int Insert(websiteResourceInfo _websiteResourceInfo)
        {
           try
            {
                return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, "sp_mc_websiteResourceInsert"                                           
											, _websiteResourceInfo.LanguageCode											
											, _websiteResourceInfo.ResourceTypeID
											, _websiteResourceInfo.ResourceName
											, _websiteResourceInfo.Description
											, _websiteResourceInfo.Creater											
											, _websiteResourceInfo.FileType
											));
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        
        /// <summary>
        /// Update websiteResource
        /// </summary>
        /// <param name="websiteResourceInfo">Entity of websiteResource</param>
        /// <returns>error code</returns>
        public static int Update(websiteResourceInfo _websiteResourceInfo)
        {            
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteResourceUpdate"
											, _websiteResourceInfo.ResourceID
											, _websiteResourceInfo.LanguageCode											
											, _websiteResourceInfo.ResourceTypeID
											, _websiteResourceInfo.ResourceName
											, _websiteResourceInfo.Description
											, _websiteResourceInfo.Creater											
											, _websiteResourceInfo.FileType											
											);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        /// <summary>
        /// Delete SchoolAdmin
        /// </summary>
        /// <param name="ResourceID">Primary Key</param>
        /// <returns>error code</returns>
        public static int Delete(int ResourceID)
        {
           try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteResourceDelete", ResourceID);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static int publish(int ItemID)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteResourcePublish", ItemID);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static int unpublish(int ItemID)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteResourceUnPublish", ItemID);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static int InsertResource(int NewsID, string Resources)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(ConnectionString, "sp_mc_websiteResourceInsertResource", NewsID, Resources);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        #endregion

        #region "Utilities method"
		
        private static void setListwebsiteResourceInfo(ref SqlDataReader dr, ref List<websiteResourceInfo> lstwebsiteResource)
        {
            websiteResourceInfo _websiteResourceInfo = null;
            while (dr.Read())
            {
                _websiteResourceInfo = new websiteResourceInfo();
                setwebsiteResourceInfo(dr, ref _websiteResourceInfo);
                lstwebsiteResource.Add(_websiteResourceInfo);
            }
        }

        private static void setwebsiteResourceInfo(SqlDataReader dr, ref websiteResourceInfo _websiteResourceInfo)
        {
			_websiteResourceInfo.ResourceID = dr[websiteResourceDAL.FIELD_RESOURCEID] != DBNull.Value ? Convert.ToInt32(dr[websiteResourceDAL.FIELD_RESOURCEID]) : 0;
			_websiteResourceInfo.LanguageCode = dr[websiteResourceDAL.FIELD_LANGUAGECODE] != DBNull.Value ? Convert.ToString(dr[websiteResourceDAL.FIELD_LANGUAGECODE]) : string.Empty;
			_websiteResourceInfo.ResourceCode = dr[websiteResourceDAL.FIELD_RESOURCECODE] != DBNull.Value ? Convert.ToString(dr[websiteResourceDAL.FIELD_RESOURCECODE]) : string.Empty;
            _websiteResourceInfo.ResourceTypeID = dr[websiteResourceDAL.FIELD_RESOURCETYPEID] != DBNull.Value ? Convert.ToInt32(dr[websiteResourceDAL.FIELD_RESOURCETYPEID]) : 0;
			_websiteResourceInfo.ResourceName = dr[websiteResourceDAL.FIELD_RESOURCENAME] != DBNull.Value ? Convert.ToString(dr[websiteResourceDAL.FIELD_RESOURCENAME]) : string.Empty;
			_websiteResourceInfo.Description = dr[websiteResourceDAL.FIELD_DESCRIPTION] != DBNull.Value ? Convert.ToString(dr[websiteResourceDAL.FIELD_DESCRIPTION]) : string.Empty;
			_websiteResourceInfo.Creater = dr[websiteResourceDAL.FIELD_CREATER] != DBNull.Value ? Convert.ToString(dr[websiteResourceDAL.FIELD_CREATER]) : string.Empty;
			_websiteResourceInfo.CreateDate = dr[websiteResourceDAL.FIELD_CREATEDATE] != DBNull.Value ? Convert.ToDateTime(dr[websiteResourceDAL.FIELD_CREATEDATE]) :DateTime.Now ;
			_websiteResourceInfo.FileType = dr[websiteResourceDAL.FIELD_FILETYPE] != DBNull.Value ? Convert.ToString(dr[websiteResourceDAL.FIELD_FILETYPE]) : string.Empty;
			_websiteResourceInfo.FileUrl = dr[websiteResourceDAL.FIELD_FILEURL] != DBNull.Value ? Convert.ToString(dr[websiteResourceDAL.FIELD_FILEURL]) : string.Empty;
            _websiteResourceInfo.Downloaded = dr[websiteResourceDAL.FIELD_DOWNLOADED] != DBNull.Value ? Convert.ToInt32(dr[websiteResourceDAL.FIELD_DOWNLOADED]) : 0;
			_websiteResourceInfo.IsActive = dr[websiteResourceDAL.FIELD_ISACTIVE] != DBNull.Value ? Convert.ToBoolean(dr[websiteResourceDAL.FIELD_ISACTIVE]) : false;
			_websiteResourceInfo.Atribute1 = dr[websiteResourceDAL.FIELD_ATRIBUTE1] != DBNull.Value ? Convert.ToString(dr[websiteResourceDAL.FIELD_ATRIBUTE1]) : string.Empty;
			_websiteResourceInfo.Atribute2 = dr[websiteResourceDAL.FIELD_ATRIBUTE2] != DBNull.Value ? Convert.ToString(dr[websiteResourceDAL.FIELD_ATRIBUTE2]) : string.Empty;
			_websiteResourceInfo.Atribute3 = dr[websiteResourceDAL.FIELD_ATRIBUTE3] != DBNull.Value ? Convert.ToString(dr[websiteResourceDAL.FIELD_ATRIBUTE3]) : string.Empty;
			                        
        }
        #endregion Methods

        #region Private Members

        private static string TABLE_NAME = "[mc_websiteResource]";

        #endregion
    }
}

		